﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using System.Configuration;
using Ext.Net;
using System.Data;
using Newtonsoft.Json;
using System.IO;
using System.Text;


namespace TCDC_System.Content.MasterData
{
    public partial class p_tcdc_mst_site : SystemPage.UIPage
    {
        private tcdc_mst_site objSite;
        protected void Page_Init(object sender, EventArgs e)
        {
            this.SiteAdd1.GridStore = StoreSite;
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            objSite = new tcdc_mst_site();
            objSite.strConn = ConfigurationManager.AppSettings["ConnString"];
            if (!X.IsAjaxRequest)
            {
                StoreSite.DataSource = GetSite();
                StoreSite.DataBind();
            }
        }

        protected DataTable GetSite()
        {
            DataTable dt = objSite.getByObject(new tcdc_mst_site());
            return dt;
        }

        protected void AddSite(object sender, DirectEventArgs e)
        {
            SiteAdd1.Show();
        }

        protected void UpdateSite(object sender, DirectEventArgs e)
        {
            string json = e.ExtraParams["SiteValue"];
            Dictionary<string, string>[] workingdays = JSON.Deserialize<Dictionary<string, string>[]>(json);
            string site_id = string.Empty;
            string site_name_th = string.Empty;
            string site_name_en = string.Empty;
            string site_short_name = string.Empty;

            foreach (Dictionary<string, string> row in workingdays)
            {
                site_id = row["site_id"].ToString();
                site_name_th = row["site_name_th"].ToString();
                site_name_en = row["site_name_en"].ToString();
                site_short_name = row["site_short_name"].ToString();
            }

            objSite.site_id = Convert.ToInt32(site_id);
            objSite.site_name_th = site_name_th;
            objSite.site_name_en = site_name_en;
            objSite.site_short_name = site_short_name;
            objSite.updateData(objSite);
            StoreSite.DataSource = GetSite();
            StoreSite.DataBind();
        }

        protected void DeleteSite(object sender, DirectEventArgs e)
        {
            this.ConfirmDeleteSite();
        }

        protected void StoreSite_Refresh(object sender, StoreRefreshDataEventArgs e)
        {
            StoreSite.DataSource = GetSite();
            StoreSite.DataBind();
        }

        protected void ExportExcelSite(object sender, EventArgs e)
        {
            string json = GridData.Value.ToString();

            DataTable dt = (DataTable)JsonConvert.DeserializeObject(json, (typeof(DataTable)));

            if (dt.Rows.Count > 0)
            {
                Response.Clear();
                Response.AddHeader("content-disposition", "attachment;filename=Site.xls");
                Response.ContentType = "application/vnd.ms-excel";
                StringWriter stringWriter = new StringWriter();
                HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWriter);
                DataGrid dataExportExcel = new DataGrid();
                dataExportExcel.DataSource = dt;
                dataExportExcel.DataBind();
                dataExportExcel.RenderControl(htmlWrite);
                StringBuilder sbResponseString = new StringBuilder();
                sbResponseString.Append("<html xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns=\"http://www.w3.org/TR/REC-html40\"> <head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></head> <body>");
                sbResponseString.Append(stringWriter + "</body></html>");
                sbResponseString.Replace("&nbsp;", "&#160;");
                Response.Write(sbResponseString.ToString());
                Response.End();
            }
        }
        [DirectMethod(Namespace = "MyNameSpace")]
        public void ConfirmDeleteSite()
        {
            X.Msg.Confirm("Confirm", "ยืนยันการลบ ?", new MessageBoxButtonsConfig
            {
                Yes = new MessageBoxButtonConfig
                {
                    Handler = "MyNameSpace.DeleteDataSite()",
                    Text = "Yes"
                },
                No = new MessageBoxButtonConfig { Text = "No" }
            }).Show();
        }
        [DirectMethod(Namespace = "MyNameSpace")]
        public void DeleteDataSite()
        {
            RowSelectionModel sm = this.GridPanelSite.SelectionModel.Primary as RowSelectionModel;
            string site_id = string.Empty;

            foreach (SelectedRow row in sm.SelectedRows)
            {
                site_id = row.RecordID;
            }
            objSite.deleteData(site_id);
            StoreSite.DataSource = GetSite();
            StoreSite.DataBind();
        }
    }
}

